/**
 *
 * @description login check
 * **/

const { ErrorResModel } =  require('../model/ResModel')
const { loginCheckFailInfo } = require('../model/ErrorInfoModel')

async function logincheck(ctx,next) {

  if (!ctx.session || !ctx.session.userInfo) {
    ctx.body = new ErrorResModel(loginCheckFailInfo)
    return
  }
  await next()
  return
}

async function loginRedirect(ctx,next) {
  console.log(ctx.session.userInfo)
  // 已登录
  if (ctx.session && ctx.session.userInfo) {
    await next()
    return
  }
  // 没有登录
  const curUrl = ctx.url
  ctx.redirect(`login?url=${encodeURIComponent(curUrl)}`)
}

module.exports = {
  logincheck,
  loginRedirect,
}